import RSASocket as rsk
from threading import Thread
from concurrent.futures import ThreadPoolExecutor


# def recv_forever(obj):
# 	while True:
# 		data = obj.conn1.recvall()
# 		if data:
# 			obj.recvedText = str(data,'utf-8')


def accept(sock):
	while True:
		conn,addr = sock.accept()
		return conn,addr


class Talk:
	def __init__(self,asServer=False,server1=('127.0.0.1',5001),server2=('127.0.0.1',5000)):
		if asServer:
			self.sock1 = rsk.Socket()
			self.sock2 = rsk.Socket()
			self.sock1.bind(('0.0.0.0',5000))
			self.sock2.bind(('0.0.0.0',5001))
			self.sock1.listen(1)
			self.sock2.listen(1)
			pool = ThreadPoolExecutor()
			t1 = pool.submit(accept,self.sock1)
			t2 = pool.submit(accept,self.sock2)
			print('启动成功，等待连接...')
			# print(t1.result())
			# pritn(t2.result())
			self.conn1,_=t1.result()
			self.conn2,_=t2.result()
			print('连接成功，可以开始聊天啦！')
		else:
			self.conn1 = rsk.Socket()
			self.conn2 = rsk.Socket()
			self.conn1.connect(server1)
			self.conn2.connect(server2)
			print('连接成功，可以开始聊天啦！')
		self.recvedText = ''

	def send(self,text):
		self.conn2.sendall(bytes(text,'utf-8'))

	def recv(self):
		data = str(self.conn1.recvall(),'utf-8')
		if data:
			self.recvedText=data

























